var isIE ; // Detect if the browser is IE or not. If it is not IE, we assume that the browser is FireFox.
var posX = 0 // Temporary variables to hold mouse x pos
var posY = 0 // Temporary variables to hold mouse y pos
var moveIt=false;
var once = false;
var nowX = 0;
var nowY = 0;
var offsetx = 0;
var offsety = 0;
var e = window.event;
var x = 0;
var y = 0;

function MouseDown()
{
     var browserName=navigator.appName;
     if (browserName=="Microsoft Internet Explorer")
     {
          isIE = true;
     }
     else
     {
          isIE = false;
     }

     // If Firefox -- that is, !IE -- then set up for mouse capture
     //if (!IE) document.captureEvents(Event.MOUSEMOVE)
     if (!isIE) document.captureEvents(Event.MOUSEDOWN)

     // Set-up to use getMouseXY function onMouseMove //document.onmousedown = getMouseXY; To capture click on the document
     var x = document.getElementsByTagName("a");
     var y = document.getElementsByTagName("img");
     var z = document.getElementsByTagName("input");
     for(i = 0; i < x.length; i++)
     {
          x[i].onmousedown = getMouseXY;
     }

     for(i = 0; i < y.length; i++)
     {
          y[i].onmousedown = getMouseXY;
     }
     for(i = 0; i < z.length; i++)
     {
          z[i].onmousedown = getMouseXY;
     }


}

// Main function to retrieve mouse x-y pos.s
function getMouseXY(event)
{

      if (isIE) // grab the x-y pos.s if browser is IE
     {
          posX = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
          posY = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;

     }
     else // grab the x-y pos.s if browser is Firefox
     {
          posX = event.pageX;
          posY = event.pageY;
     }

     // catch possible negative values in NS4
     if (posX < 0){
          posX = 0
          }
     if (posY < 0){
          posY = 0
          }
     return true
}

function getBrowser()
{
     var isIE;
     var browserName=navigator.appName;
     if (browserName=="Microsoft Internet Explorer")
     {
          isIE = true;
     }
     else
     {
          isIE = false;
     }
     return isIE;
}

function mousePressed(event) {
     //alert("inside mouse Pressed " + istIE);
     moveIt = true;
     once = false;
     getBrowser();

     if(!isIE) {
          document.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
     }

     var div = document.getElementById("theLayer");

     nowX = parseInt(div.style.left);
     nowY = parseInt(div.style.top);

     document.onmousemove = getMousePressed;
     document.onmouseup = mouseReleased;

}

function getMousePressed(event) {
     if(!once) {
          once = true;
          offsetx = isIE ? window.event.clientX : event.pageX;
          offsety = isIE ? window.event.clientY : event.pageY;
     }

     if(moveIt) {
          var div = document.getElementById("theLayer");

          if(isIE) {
               if(window.event.clientX > 0 && window.event.clientX < 900)
                    x = window.event.clientX
               else if(window.event.clientX < 5)
                    x = 250;
               else if(window.event.clientX > 900 )
                    x = 800;

               if(window.event.clientY > 0)
                    y = window.event.clientY;
               else
                    y = 20;
          }
          else {
               if(event.pageX > 0 && event.pageY < 900)
                    x = event.pageX;
               else if(event.pageX < 5)
                    x = 250;
               else if(event.pageX > 900 )
                    x = 800;

               if(event.pageY > 0)
                    y = event.pageY;
               else
                    y = 20;
          }


          div.style.left = nowX + x - offsetx + "px";
          div.style.top = nowY + y - offsety + "px"
     //div.style.left = 50 + x - offsetx + "px";
     //div.style.top = 50 + y - offsety + "px";
     }
}

function mouseReleased(event) {
     moveIt = false;

     if(!isIE) {
          document.releaseEvents(Event.MOUSEMOVE | Event.MOUSEUP);
     }
}